﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using STF.OrangeDog.IService;
using STF.OrangeDog.IRepository;
using STF.OrangeDog.Dtos;
using STF.OrangeDog.Model;

namespace STF.OrangeDog.Service
{
    public class CoverBll:ICoverBll
    {
        private ICoverDal _dal;

        public CoverBll(ICoverDal dal)
        {
            _dal = dal;
        }
        public async Task<int> AddTheCoverAsync(string Details, string Image1, string Image2, string Image3, int Bid)
        {
            return await _dal.AddModelAsync(new Cover
            {
                details = Details,
                Image1 = Image1,
                Image2 = Image2,
                Image3 = Image3,
                bid = Bid
            });
        }

        public async Task<int> DeleteTheCoverAsync(int Id)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            if (data.DeleteId == 1)
                return 0;
            return await _dal.RemoveModelAsync(data);
        }

        public async Task<int> EditTheCoverAsync(int Id, string Details, string Image1, string Image2, string Image3, int Bid)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            data.details = Details;
            data.Image1 = Image1;
            data.Image2 = Image2;
            data.Image3 = Image3;
            data.bid = Bid;
            data.UpdateTime = DateTime.Now;
            return await _dal.EditModelAsync(data);
        }

        public async Task<IEnumerable<CoverDtos>> GetAll(int DeleteId = 1)
        {
            var data = await _dal.GetDataByChooseAsync(r => r.DeleteId == DeleteId);
            return data != null ? data.Select(r => new CoverDtos
            {
                id = r.Id,
                details = r.details,
                Image1 = r.Image1,
                Image2 = r.Image2,
                Image3 = r.Image3,
                bid = r.bid,
              
            }) : new List<CoverDtos>();
        }

        public async Task<CoverDtos> GetDataById(int Id)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            return data == null ? new CoverDtos() : new CoverDtos
            {
                id = data.Id,
                details = data.details,
                Image1 = data.Image1,
                Image2 = data.Image2,
                Image3 = data.Image3,
                bid = data.bid,
                
            };
        }
    }
}
